<?php
namespace Admin\Controller;
use Think\Controller;
class ModelsController extends IndexController {
    public function lists(){
        $this->display();
    }
    public function model_list(){
        $model=D("Model");
        if(I("request.iDisplayStart")==''){
            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }
        if(I("request.sSearch")!=''){
            $where['name'] = array('like','%'.I("request.sSearch").'%');;
        }else{
            $where='';
        }

        $result=$model->field("id,name,is_cd,cd_id,controller,function,ico")
            ->where($where)
            ->limit($start,I("request.iDisplayLength"))
            ->select();
        if(!$result) {
            echo json_encode(0);exit;
        }

        $ret = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $start,
            "iTotalDisplayRecords" => $model->where($where)->count(),
            "aaData" => $result
        );
        
        echo json_encode($ret);
    }
    public function add(){
        $this->display();
        
    }
    //添加
    public function doadd(){
        $name=I('request.name');
        $is_cd=I('request.is_cd');
        $cd_id=I('request.cd_id');
        $controller=I('request.controller');
        $function=I('request.function');
        $ico=I('request.ico');
        $model=D("Model");
        $data['name']=$name;
        $data['is_cd']=$is_cd;
        $data['cd_id']=$cd_id;
        $data['controller']=$controller;
        $data['function']=$function;
        $data['ico']=$ico;
        $res=$model->add($data);
        if(!$res){
            echo json_encode(0);
        }else{
            echo json_encode(1);
        }
    }
    public function model_pid(){
        $model=D("Model");
        $res=$model->where("cd_id=0")->field("id,name")->select();
        echo json_encode($res);
        
    }
    
}